package com.example.aiyou.passwordmanage.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.example.aiyou.passwordmanage.R;

public class UserActivity extends AppCompatActivity {
    private EditText editAccount;
    private EditText editPhone;
    private EditText editNick;
    private AutoCompleteTextView editEmail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        editAccount = (EditText) findViewById(R.id.edit_account);
        editPhone = (EditText) findViewById(R.id.edit_phone);
        editNick = (EditText) findViewById(R.id.edit_nick);
        editEmail = (AutoCompleteTextView)findViewById(R.id.edit_email);
        Button button = (Button) findViewById(R.id.button_save);
        showInfo();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveInfo();
            }
        });
    }
    public static void actionStart(Context context){
        Intent intent = new Intent(context,UserActivity.class);
        context.startActivity(intent);
    }
    public void showInfo(){
        SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone","");
        String email = sharedPreferences.getString("email","");
        String account = sharedPreferences.getString("account","");
        String nick = sharedPreferences.getString("nick","");
        if(!TextUtils.isEmpty(phone)){
            editPhone.setText(phone.trim());
        }
        if(!TextUtils.isEmpty(email)){
            editEmail.setText(email.trim());
        }
        if(!TextUtils.isEmpty(account)){
            editAccount.setText(account.trim());
        }
        if(!TextUtils.isEmpty(nick)){
            editNick.setText(nick.trim());
        }
    }
    public void saveInfo(){
        SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
        editor.putString("account",editAccount.getText().toString().trim());
        editor.putString("phone",editPhone.getText().toString());
        editor.putString("email",editEmail.getText().toString());
        editor.putString("nick",editNick.getText().toString());
        editor.apply();
        Intent i = new Intent(UserActivity.this,SettingsActivity.class);
        i.putExtra("meg","success");
        startActivity(i);
        finish();
    }
}
